<?php
/*
 * Created on 20.12.2005
 *
 */
 
class FilterChain 
{

    /**
     * Points to a position in the chain
     */
    var $pointer = 0;
    /**
     * holds the chain
     */
    var $chain = array();
    /**
     * Applications basedir
     */
     var $basedir;
     /**
      * Filter config
      */
     var $config;

    function FilterChain (& $env, $basedir, & $config) 
    {
        $this->env = & $env;
        $this->config = & $config;
    }
    
    /**
     * Add a filter to the chain
     * 
     * A filter 
     * 
     * @param Filter filter Filter class to register - order is relevant
     */
    function addFilter ($filter) 
    {
        $this->chain[] = $filter;
    }
    
    /**
     * Start the filter chain
     */
    function startChain () 
    {
        $this->doFilter($this->env);
    }
    
    function doFilter (& $env) 
    {
        if (count( $this->chain ) <= $this->pointer)
            return;
        $filterfile = $this->chain[$this->pointer].$this->config['classfileending'];
        $matches = array();
        preg_match('/(\w+)$/',$this->chain[$this->pointer], $matches);
        $filterclass = $matches[1];
        $this->pointer++;
        include_once ($filterfile);
        $obj = new $filterclass();
        $obj->init($this->config);
        $obj->doFilter($env, $this);
    }

}
 
?>
